package pro.lepers.portfolio.view.contents.portfolio.content {
	import pro.lepers.portfolio.event.RenderEvent;
	import fr.digitas.flowearth.command.IBatchable;

	import pro.lepers.portfolio.Site;
	import pro.lepers.portfolio.core.master;
	import pro.lepers.portfolio.event.MasterModelEvent;
	import pro.lepers.portfolio.view.contents.portfolio.data.contents.AbstractContentData;
	import pro.lepers.portfolio.view.misc.OverLayer;
	import pro.lepers.portfolio.view.misc.cursor.Cursor;
	import pro.lepers.portfolio.view.misc.cursor.CursorType;

	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.geom.Rectangle;
	import flash.utils.Timer;

	/**
	 * @author Pierre Lepers
	 */
	public class ThumbContent extends Sprite {

		
		
		public function ThumbContent( pcData : AbstractContentData ) {
			_contentData = pcData;
//			transform.colorTransform = highQuality ? new ColorTransform(): new ColorTransform( 1, 0, 0 );	
		}
		
		
		public function dispose () : void {
			_contentData = null;
			if( _flyOver ) 
				_flyOver = false;
			
			callRender( false );
			
		}

		public function getLoadables() : IBatchable {
			return null;
		}
		
		public function setSize( w :int, h : int ) : void {
			_width = w;
			_height = h;
		}
		

		public function unfocused () : void {
			_focused = false;
			if( _flyOver )
				flyOver = false;
		}

		public function focused () : void {
			_focused = true;
		}

		public function unfocus () : void {
		}

		public function focus () : void {
		}
		
		
		public function hidded () : void {
			_hidded = true;
			master.removeEventListener( MasterModelEvent.UP_SCALED, onUpScaled );
		}

		public function shown () : void {
			_hidded = false;
		}
		
		public function set highQuality(highQuality : Boolean) : void {
			_highQuality = highQuality;
		}
		
		public function get highQuality() : Boolean {
			return _highQuality;
		}

		
		public function get flyOver () : Boolean {
			return _flyOver;
		}
		
		public function set flyOver (flyOver : Boolean) : void {
			if( _flyOver == flyOver )return;
			_flyOver = flyOver;
			
			if( _flyOver ) {
				OverLayer.instance.addChild( this );
				stage.addEventListener( MouseEvent.MOUSE_DOWN, downOutside );
				stage.addEventListener( KeyboardEvent.KEY_DOWN, downOutside );
				addEventListener( MouseEvent.MOUSE_DOWN, lockDownStage );
				Site.scaledContainer.addEventListener( MouseEvent.MOUSE_OVER, addCloseCursor );
				Site.scaledContainer.addEventListener( MouseEvent.MOUSE_OUT, removeCloseCursor );
				
				master.addEventListener( MasterModelEvent.UP_SCALED, onUpScaled );
				master.downScale();
			}
			else {
				stage.removeEventListener( MouseEvent.MOUSE_DOWN, downOutside );
				stage.removeEventListener( KeyboardEvent.KEY_DOWN, downOutside );
				removeEventListener( MouseEvent.MOUSE_DOWN, lockDownStage );
				Site.scaledContainer.removeEventListener( MouseEvent.MOUSE_OVER, addCloseCursor );
				Site.scaledContainer.removeEventListener( MouseEvent.MOUSE_OUT, removeCloseCursor );
				removeCloseCursor();
				
				master.upScale();
			}
		}

		//_____________________________________________________________________________
		//																 FlyOut Methods

		private function lockDownStage (event : MouseEvent) : void {
			event.stopPropagation();
		}

		private function downOutside ( event : Event = null ) : void {
			flyOver = false;
		}

		
		private function addCloseCursor (event : MouseEvent = null ) : void {
			Cursor.setCursor( CursorType.CLOSE );
		}


		private function removeCloseCursor (event : MouseEvent = null ) : void {
			Cursor.removeCursor( CursorType.CLOSE );
		}

		private function onUpScaled (event : MasterModelEvent) : void {
			master.removeEventListener( MasterModelEvent.UP_SCALED, onUpScaled );
			OverLayer.instance.retreiveChild( this );
		}


		//_____________________________________________________________________________
		//																		 render

		protected function callRender ( flag : Boolean ) : void {
			if( flag && ! _renderTick ) {
				_renderTick = new Timer( 40 );
				_renderTick.start();
				_renderTick.addEventListener(TimerEvent.TIMER, _onRenderTick );
			} else if( _renderTick ) {
					_renderTick.removeEventListener( TimerEvent.TIMER, _onRenderTick );
					_renderTick.stop();
					_renderTick = null;
				
			}
		}

		private function _onRenderTick (event : TimerEvent) : void {
			setRenderZone();
			dispatchEvent( new RenderEvent(RenderEvent.REDRAW, _renderZone  ) );
		}

		protected function setRenderZone () : void {
			_renderZone = getBounds( Site.scaledContainer );
		}

		protected var _renderZone : Rectangle = new Rectangle();
		private var _renderTick : Timer;

		
		
		protected var _highQuality : Boolean;
		
		protected var _width : int;
		protected var _height : int;
		
		protected var _focused : Boolean = false;
		protected var _hidded : Boolean = true;
		
		protected var _contentData : AbstractContentData;
		
		private var _flyOver : Boolean = false;

	}
}
